package com.shundr.shipper.truck;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.common.model.AttributeInfo;
import com.shundr.shipper.common.util.AllCapTransfMethod;
import com.shundr.shipper.common.util.AttrUtil;
import com.shundr.shipper.common.util.ProgressDialogHelper;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.CListView;
import com.shundr.shipper.common.view.DialogChoiceTruckInfo;
import com.shundr.shipper.common.view.DialogChoiceTruckInfo.OnChoiceListener;
import com.shundr.shipper.frame.http.LHttpRequest;
import com.shundr.shipper.frame.util.StringUtil;
import com.shundr.shipper.truck.adapter.FamTruckAdapter;
import com.shundr.shipper.truck.adapter.FamTruckAdapter.OnFamAddListener;
import com.shundr.shipper.truck.http.UserTruckRequest;
import com.shundr.shipper.truck.model.TruckInfo;

public class AddFamTruckActivity extends BaseActivity implements OnClickListener{
    
	private List<TruckInfo> mList = new ArrayList<TruckInfo>();

	private FamTruckAdapter mAdapter; 
	
	private CListView mListView;
	private RelativeLayout mLayoutNoContent;
	private TextView tvNoContent;
	
	private Button btnSearch;
	
	private Button btnPCode;
	
	private LinearLayout layoutPlate;
	private EditText etMobile;
	
	private EditText etPlate;
	
	private RadioGroup radioGroup;
	private int flag = 0;
	
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case UserTruckRequest.GET_SEARCH_FAM_TRUCK_LIST_SUCCESSED:
					@SuppressWarnings("unchecked")
					List<TruckInfo> infos = (List<TruckInfo>)msg.obj;
					if (infos != null) {
						if (infos.size() > 0) {
							mList.clear();
							mListView.setVisibility(View.VISIBLE);
							mLayoutNoContent.setVisibility(View.GONE);
							mList.addAll(infos);
							mAdapter.notifyDataSetChanged();
						} else {
							mListView.setVisibility(View.GONE);
						    mLayoutNoContent.setVisibility(View.VISIBLE);
						}
					}
					ProgressDialogHelper.dismiss();
					mListView.onRefreshComplete();
					mListView.onLoadMoreComplete();
					break;
				case LHttpRequest.REQUEST_FAILED:
					mListView.onLoadMoreComplete();
					mListView.onRefreshComplete();
					ProgressDialogHelper.dismiss();	
					ToastHelper.toastShort(mContext, msg.obj.toString());
					break;

				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_fam_truck);
		initView();
	}
	
	private void initView(){
		mListView = (CListView) findViewById(R.id.lv_list);
		mLayoutNoContent = (RelativeLayout) findViewById(R.id.layout_no_content);
		tvNoContent = (TextView) findViewById(R.id.tv_no_content);
		tvNoContent.setText("您还未添加熟车,点我添加");
		mAdapter = new FamTruckAdapter(mContext,mList);
		mAdapter.setAddListener(new OnFamAddListener() {
			
			@Override
			public void onAdd() {
				setResult(888);
			}
		});
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				try {
					Intent intent = new Intent(mContext, TruckCommonDetailActivity.class);
					intent.putExtra("truckId", mList.get(position - 1).getTruckId());
					intent.putExtra("id", mList.get(position - 1).getTruckId());
					startActivityForResult(intent, 888);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		});
		
		btnSearch = (Button) findViewById(R.id.btn_search);
		btnSearch.setOnClickListener(this);
		
		btnPCode=(Button)findViewById(R.id.btn_province_code);
		btnPCode.setOnClickListener(this);
		
		layoutPlate = (LinearLayout)findViewById(R.id.layout_plate);
		etMobile = (EditText)findViewById(R.id.et_mobile);
		etPlate = (EditText)findViewById(R.id.et_plate);
		etPlate.setTransformationMethod(new AllCapTransfMethod());
		
		radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.radio_plate:
					layoutPlate.setVisibility(View.VISIBLE);
					etMobile.setVisibility(View.GONE);
					flag = 0;
					break;
				case R.id.radio_mobile:
					layoutPlate.setVisibility(View.GONE);
					etMobile.setVisibility(View.VISIBLE);
					flag = 1;
					break;

				default:
					break;
				}
			}
		});
	}
	
	private boolean check(){
		if(flag == 0){
			if(StringUtil.isNull(btnPCode.getText().toString())){
				ToastHelper.toastShort(mContext, "请选择省份");
				return false;
			}
			if(StringUtil.isNull(etPlate.getText().toString())){
				ToastHelper.toastShort(mContext, "请输入车牌号");
				return false;
			}
			String pCode = btnPCode.getText().toString().trim();
			String truckNum=etPlate.getText().toString().trim();
			if(!StringUtil.isTruckNum(pCode+truckNum)){
				ToastHelper.toastShort(mContext, "请输入正确的车牌号码");
				return false;
			}
		}else{
			if(StringUtil.isNull(etMobile.getText().toString())){
				ToastHelper.toastShort(mContext, "请输入手机号码");
				return false;
			}
			if(!StringUtil.isMobileNO(etMobile.getText().toString())){
				ToastHelper.toastShort(mContext, "请输入正确的手机号码");
				return false;
			}
		}
		return true;
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_search:
			if(check()){
				if(flag == 0){
					String pCode = btnPCode.getText().toString().trim();
					String truckNum=etPlate.getText().toString().trim();
					ProgressDialogHelper.show(mContext, "正在加载中...");
					new UserTruckRequest(mContext, mHandler).getSearchFamTruckList("", pCode + truckNum);
				}else{
					String mobile = etMobile.getText().toString().trim();
					new UserTruckRequest(mContext, mHandler).getSearchFamTruckList(mobile, "");
				}
			}
			break;
		case R.id.btn_province_code:
			DialogChoiceTruckInfo choiceTruckNum = new DialogChoiceTruckInfo(
					mContext, "选择车牌", AttrUtil.TYPE_PROVINCE_SHORT,
					new OnChoiceListener() {

						@Override
						public void selected(AttributeInfo info) {
							try {
								btnPCode.setText(info.getName());
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					});
			choiceTruckNum.show();
			break;

		default:
			break;
		}
	}
	
	
	
}
